using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/**武器 */
public class Weapon : MonoBehaviour
{
    /**武器图片集合 */
    public Sprite[] weaponImgs;
    
    /**子弹预制体 */
    public GameObject bulletPrefab;
    
    /**左对象 */
    public Transform leftObj;
    
    /**右对象 */
    public Transform rightObj;
    
    /**武器类型 */
    private GameWeapon _type = GameWeapon.Null;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    
    public void SetType(GameWeapon type)
    {
        if (_type == type)
        {
            return;
        }
        _type = type;
        GetComponent<SpriteRenderer>().sprite = weaponImgs[(int) type];
    }

    /**攻击 */
    public void Attack()
    {
        switch (_type)
        {
            case GameWeapon.Staff:
                var position = transform.position;
                var parentX = transform.parent.position.x;
                
                var bulletData = new BulletData();
                bulletData.speed = transform.parent.localScale.x > 0 ?  10.0f : -10.0f;
                bulletData.endPos = new Vector3(
                    parentX + (transform.parent.localScale.x > 0 ? rightObj.localPosition.x : leftObj.localPosition.x), 
                    position.y, position.z);
            
                BulletMgr.instance.FireBullet(bulletPrefab, position, bulletData);
                return;
        }
    }
}
